/********************************************************
 * Filename: TUnitFileCompare.java
 * Author: LMU
 * Program: TUnit
 * Description: Compares two files for equality.    
 * Revision History:
 * 20060907: Initial Revision.
 * 20060911: Updated to compare files line by line.  
 * *****************************************************/

package edu.lmu.cs.tUnit.test;

import java.io.*;

public class TUnitFileCompare {

	/**
	 * Returns <code>true</code> if both input streams byte contents is identical.
	 *
	 * @param input1
	 * first input to contents compare
	 * @param input2
	 * second input to contents compare
	 * @return <code>true</code> if content is equal
	 */
	boolean contentsEqual( BufferedReader b1, BufferedReader b2 ) {
		boolean returnValue = false;
		try {
			if( b1 == b2 )
				return true;

			// no byte contents
			if( b1 == null && b2 == null ) 
				return true;

			// only one has contents
			if( b1 == null || b2 == null )
				return false;

			while( true ) {
				String c1 = b1.readLine();
				
				String c2 = b2.readLine();

				if( c1 == null && c2 == null ) {
					returnValue = true;
					break;
				}

				if( c1.compareTo(c2) != 0)
					break;
			}
		}
		catch( IOException ex ) {
			return false;
		}

		finally {
			try {
				try {
					if( b1 != null ) {
						b1.close();
					}
				}
				finally {
					if( b2 != null ) {
						b2.close();
					}
				}
			}
			catch( IOException e ) {
				return false;
			}
		}

		return returnValue;
	}
	
	/**
	 * Returns <code>true</code> if both input streams byte contents is identical.
	 *
	 * @param input1
	 * first input to contents compare
	 * @param input2
	 * second input to contents compare
	 * @param line numbers to skip
	 * @return <code>true</code> if content is equal
	 */
	boolean contentsEqual( BufferedReader b1, BufferedReader b2, int[] lineNums ) {
		boolean returnValue = false;
		String c1, c2;
		try {
			if( b1 == b2 )
				return true;

			// no byte contents
			if( b1 == null && b2 == null ) 
				return true;

			// only one has contents
			if( b1 == null || b2 == null )
				return false;

			int line = 0;
			boolean found = false;
			while( true ) {
				line++;
				
				for(int i = 0; i < lineNums.length; i++) {
					if(line == lineNums[i]) {
						found = true;
					}
				}
				
				if (found) {
					c1 = b1.readLine();
					c2 = b2.readLine();
					found = false;
				}
				
				else {
					c1 = b1.readLine();
					c2 = b2.readLine();

					if( c1 == null && c2 == null ) {
						returnValue = true;
						break;
					}

					if( c1.compareTo(c2) != 0)
						break;
				}
			}
		}
		catch( IOException ex ) {
			return false;
		}

		finally {
			try {
				try {
					if( b1 != null ) {
						b1.close();
					}
				}
				finally {
					if( b2 != null ) {
						b2.close();
					}
				}
			}
			catch( IOException e ) {
				return false;
			}
		}

		return returnValue;
	}
}